import { injectable, inject } from 'tsyringe';
import { IChatService, ChatMessage } from '../../domain/interfaces/IChatService';
import { MessageRepositoryToken } from '../../domain/interfaces/IMessageRepository';
import type { IMessageRepository } from '../../domain/interfaces/IMessageRepository';
import { WebSocketManagerToken } from '../../domain/interfaces/IWebSocketManager';
import type { IWebSocketManager } from '../../domain/interfaces/IWebSocketManager';
import { LoggerToken } from '../../domain/interfaces/ILogger';
import type { ILogger } from '../../domain/interfaces/ILogger';

@injectable()
export class ChatService implements IChatService {
  constructor(
    @inject(MessageRepositoryToken) private messageRepository: IMessageRepository,
    @inject(WebSocketManagerToken) private wsManager: IWebSocketManager,
    @inject(LoggerToken) private logger: ILogger
  ) {}

  async sendMessage(roomId: string, message: ChatMessage): Promise<void> {
    // 保存到数据库
    await this.messageRepository.save(message);
    
    // 广播到房间
    this.wsManager.broadcastToRoom(roomId, {
      event: 'chat:message',
      data: message,
      timestamp: Date.now()
    });

    this.logger.debug('Message broadcasted', { roomId, messageId: message.id });
  }

  async joinRoom(roomId: string, user: any): Promise<void> {
    // 实现加入房间逻辑
    this.logger.info('User joining room', { roomId, userId: user.userId });
  }

  async leaveRoom(roomId: string, userId: string): Promise<void> {
    // 实现离开房间逻辑
    this.logger.info('User leaving room', { roomId, userId });
  }

  async getRoomParticipants(roomId: string): Promise<any[]> {
    return [];
  }
}
